package com.hao.dispatcher;

import com.hao.model.QueueManager;
import com.hao.model.PCBProcess;

import java.util.Comparator;
import java.util.LinkedList;

/**
 * 最短进程优先。非抢占策略
 */
public class SPNDispatcher implements Dispatcher {

    @Override
    public PCBProcess doDispatcher(QueueManager queueManager) {
        if (queueManager.getReadyQueue().size() <= 0) {
            return null;
        }

        LinkedList<PCBProcess> readyQueue = queueManager.getReadyQueue();
        readyQueue.sort(new Comparator<PCBProcess>() {
            @Override
            public int compare(PCBProcess pcbProcess1, PCBProcess pcbProcess2) {
                //@todo 如果可以，在时间相等的情况下可以考虑优先级
                return pcbProcess1.getTime() - pcbProcess2.getTime();//运行时间从小到大
            }
        });

        return readyQueue.poll();
    }
}
